package manage.modules;

import java.util.List;
import java.util.Map;

import manage.commons.cache.Cache;
import manage.commons.utils.CommonUtil;
import manage.commons.utils.DBUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

public class BaseServiceImpl implements BaseService {
	private static final Logger log = Logger.getLogger(BaseServiceImpl.class);
	private static final int DEFAULT_CACHE_TIME = 60;
	private NamedParameterJdbcTemplate queryJdbc;
	private Cache defaultCache;
	private AbstractDao abstractDao;

	@Override
	public boolean addCache(final String cacheKey, final Object value,
			final int exp) {
		log.info("add cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value) + "---exp time ==> "
				+ exp);
		return defaultCache.add(cacheKey, exp, value);
	}

	@Override
	public boolean addCache(final String cacheKey, final Object value) {
		log.info("set cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value));
		return this.addCache(cacheKey, value, DEFAULT_CACHE_TIME);
	}

	@Override
	public boolean addOriginalCache(final String cacheKey, final Object value,
			final int exp) {
		log.info("addOriginal cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value) + "---exp time ==> "
				+ exp);
		return defaultCache.addOriginal(cacheKey, exp, value);
	}

	@Override
	public void setCache(final String cacheKey, final Object value,
			final int exp) {
		log.info("set cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value) + "---exp time ==> "
				+ exp);
		if (StringUtils.isNotBlank(cacheKey)) {
			defaultCache.set(cacheKey, exp, value);
		}
	}

	@Override
	public void setCache(final String cacheKey, final Object value) {
		log.info("set cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value));
		this.setCache(cacheKey, value, DEFAULT_CACHE_TIME);
	}

	@Override
	public void setOriginalCache(final String cacheKey, final Object value) {
		log.info("addOriginal cache ==>" + cacheKey + "----value ==>"
				+ CommonUtil.beanPropertisToString(value));
		defaultCache.setOriginal(cacheKey, value);
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T getOriginalCache(final String cacheKey) {
		log.info("get cache ==>" + cacheKey);
		if (StringUtils.isNotBlank(cacheKey)) {
			return (T)defaultCache.getOriginal(cacheKey);
		}
		return null;
	}

	@Override
	public <T> T getCache(final String cacheKey, Class<T> clazz) {
		log.info("get cache ==>" + cacheKey + "-----" + clazz.getSimpleName());
		return (T)defaultCache.get(cacheKey, clazz);
	}

	@Override
	public <T> List<T> getListByCache(final String cacheKey, Class<T> clazz) {
		log.info("get cache to list==>" + cacheKey + "-----"
				+ clazz.getSimpleName());
		if (StringUtils.isNotBlank(cacheKey)) {
			return defaultCache.getList(cacheKey, clazz);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getListMapByCache(final String cacheKey) {
		log.info("get cache to ListMap==>" + cacheKey);
		if (StringUtils.isNotBlank(cacheKey)) {
			return defaultCache.getListMap(cacheKey);
		}
		return null;
	}

	@Override
	public void clearCache(final String cacheKey) {
		log.info("delete cache ==>" + cacheKey);
		if (StringUtils.isNotBlank(cacheKey)) {
			defaultCache.delete(cacheKey);
		}
	}

	@Override
	public boolean existsCache(final String cacheKey) {
		log.info("check exists cache ==>" + cacheKey);
		if (StringUtils.isNotBlank(cacheKey)) {
			return defaultCache.exists(cacheKey);
		}
		return false;
	}

	@Override
	public <T> List<T> listByFields(Map<String, Object> fields, Class<T> clazz) {
		return abstractDao.listByFields(fields, clazz);
	}

	@Override
	public <T> List<T> listByFields(Map<String, Object> fields, Class<T> clazz,
			int rowIndex, int rowSize) {
		return abstractDao.listByFields(fields, clazz, rowIndex, rowSize);
	}

	@Override
	public <T> List<T> listByField(String field, Object value, Class<T> clazz) {
		return abstractDao.listByField(field, value, clazz);
	}

	@Override
	public <T> List<T> listByField(String field, Object value, Class<T> clazz,
			int rowIndex, int rowSize) {
		return abstractDao.listByFieldLike(field, value, clazz, rowIndex,
				rowSize);
	}

	@Override
	public <T> List<T> listByFieldLike(String field, Object value,
			Class<T> clazz) {
		return abstractDao.listByFieldLike(field, value, clazz);
	}

	@Override
	public <T> List<T> listByFieldLike(String field, Object value,
			Class<T> clazz, int rowIndex, int rowSize) {
		return abstractDao.listByFieldLike(field, value, clazz, rowIndex,
				rowSize);
	}

	@Override
	public <T> List<T> listByFieldRandom(String field, Object value,
			Class<T> clazz, int rowSize) {
		return abstractDao.listByFieldRandom(field, value, clazz, rowSize);
	}

	@Override
	public <T> List<T> listAll(Class<T> clazz) {
		return abstractDao.listAll(clazz);
	}

	@Override
	public <T> T select(String field, Object value, Class<T> clazz) {
		return abstractDao.select(field, value, clazz);
	}

	@Override
	public <T> T select(Map<String, Object> conditions, Class<T> clazz) {
		return abstractDao.select(conditions, clazz);
	}

	@Override
	public <T> int insert(T entity) {
		return abstractDao.insert(entity);
	}

	@Override
	public <T> long insertFetchId(T entity) {
		return abstractDao.insertFetchId(entity);
	}

	@Override
	public <T> int replace(T entity) {
		return abstractDao.replace(entity);
	}

	@Override
	public <T> int updateFields(String field, Object value, T entity) {
		return abstractDao.updateFields(field, value, entity);
	}

	@Override
	public <T> int updateFields(Map<String, Object> fields,
			Map<String, Object> conditions, Class<T> clazz) {
		return abstractDao.updateFields(fields, conditions, clazz);
	}

	@Override
	public <T> int delete(String field, Object value, Class<T> clazz) {
		return abstractDao.delete(field, value, clazz);
	}

	@Override
	public <T> int deleteByFields(Map<String, Object> fields, Class<T> clazz) {
		return abstractDao.deleteByFields(fields, clazz);
	}

	@Override
	public <T> int deleteByFields(Map<String, Object> fields, Class<T> clazz,
			int rowSize) {
		return abstractDao.deleteByFields(fields, clazz, rowSize);
	}

	@Override
	public <T> boolean exists(String field, Object value, Class<T> clazz) {
		return abstractDao.exists(field, value, clazz);
	}

	@Override
	public <T> boolean exists(Map<String, Object> fields, Class<T> clazz) {
		return abstractDao.exists(fields, clazz);
	}

	@Override
	public <T> Long countOfAll(Class<T> clazz) {
		return abstractDao.countOfAll(clazz);
	}

	@Override
	public <T> Long countByField(String field, String value, Class<T> clazz) {
		return abstractDao.countByField(field, value, clazz);
	}

	@Override
	public <T> Long countByFields(Map<String, Object> fields, Class<T> clazz) {
		return abstractDao.countByFields(fields, clazz);
	}

	@Override
	public NamedParameterJdbcTemplate getNamedJdbc() {
		return queryJdbc;
	}

	@Override
	public JdbcOperations getJdbcOperations() {
		return queryJdbc.getJdbcOperations();
	}

	@Override
	public String getCountSql(String sql) {
		return DBUtils.getCountSql(sql);
	}

	@Override
	public <T> Map<String, Object> comparePojo(T src, T dest) {
		return DBUtils.comparePojo(src, dest);
	}

	public void setQueryJdbc(NamedParameterJdbcTemplate queryJdbc) {
		this.queryJdbc = queryJdbc;
	}

	public void setDefaultCache(Cache defaultCache) {
		this.defaultCache = defaultCache;
	}

	public void setAbstractDao(AbstractDao abstractDao) {
		this.abstractDao = abstractDao;
	}

}
